home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / Mesa-3.0 / BeOS / GENERIC.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-01  |  3.1 KB  |  155 lines

  1. /* generic.c */
  2.  
  3. /* Demo of BEOS Mesa rendering */
  4.  
  5. /*
  6.  * See Mesa/include/GL/osmesa.h for documentation of the OSMesa functions.
  7.  *
  8.  * If you want to render BIG images you'll probably have to increase
  9.  * MAX_WIDTH and MAX_HEIGHT in src/config.h.
  10.  *
  11.  * This program is in the public domain.
  12.  *
  13.  * BEOS output provided by Tinic Urou
  14.  * 5uro@informatik.uni-hamburg.de
  15.  */
  16.  
  17. #include <AppKit.h>
  18. #include <InterfaceKit.h>
  19. #include <KernelKit.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "GL/osmesa.h"
  24.  
  25. /* imported from demo code */
  26.  
  27. extern     int             gl_width,gl_height;
  28. extern     void             render_image(void);
  29.  
  30. const     ulong             APP_SIGNATURE = '????';
  31.  
  32. class                     MesaWindow;
  33. class                     MesaView;
  34. class                    MesaApp;
  35.  
  36. // Lets make our life easy and make them global:
  37.  
  38. BBitmap                 *the_bitmap;
  39. MesaView                *the_view;    
  40. MesaWindow                 *the_window;
  41.  
  42. class MesaView : public BView
  43. {
  44.     public:
  45.     
  46.     MesaView(BRect frame):BView(frame,"Mesa View",B_FOLLOW_NONE,B_WILL_DRAW)
  47.     {
  48.     };
  49.     
  50.     void Draw(BRect frame)
  51.     {
  52.         DrawBitmap(the_bitmap,BPoint(0,0));
  53.     };                    
  54. };
  55.  
  56. class MesaWindow : public BWindow 
  57. {
  58.     public:
  59.     
  60.     MesaWindow(int width, int height):BWindow(BRect(0,0,width-1,height-1),"MesaView",B_TITLED_WINDOW,B_NOT_RESIZABLE|B_NOT_ZOOMABLE)
  61.     {
  62.         // Move window to right position
  63.         MoveTo(80, 24);
  64.         // Create bitmap view
  65.         Lock();
  66.         AddChild(the_view = new MesaView(BRect(0, 0, (gl_width)-1, (gl_height)-1)));
  67.         Unlock();
  68.     };
  69.     
  70.     bool QuitRequested(void)
  71.     {
  72.         be_app->PostMessage(B_QUIT_REQUESTED);
  73.         return TRUE;
  74.     };
  75. };
  76.  
  77. class MesaApp : public BApplication 
  78. {
  79.     public:
  80.     
  81.     MesaApp():BApplication(APP_SIGNATURE)
  82.     {
  83.         the_window = NULL;
  84.         the_bitmap = NULL;
  85.     };
  86.             
  87.     void ReadyToRun(void)
  88.     {
  89.         // Allocate the bitmap        
  90.         the_bitmap = new BBitmap(BRect(0, 0, gl_width-1, gl_height-1), B_RGB_32_BIT);
  91.  
  92.         uchar *bits = (uchar *)the_bitmap->Bits();
  93.  
  94.         memset(bits,0,the_bitmap->BytesPerRow()*gl_height);
  95.  
  96.         // Open window
  97.         the_window = new MesaWindow((gl_width),(gl_height));
  98.         the_window->Show();
  99.  
  100.         OSMesaContext ctx;
  101.         unsigned char *buffer;
  102.         double start,end;
  103.  
  104.         // Create an RGBA-mode context 
  105.         ctx = OSMesaCreateContext( GL_RGBA, NULL );
  106.  
  107.         // Bind the buffer to the context and make it current */
  108.         OSMesaMakeCurrent( ctx, bits, GL_UNSIGNED_BYTE, gl_width, gl_height );
  109.         OSMesaPixelStore( OSMESA_Y_UP, 0 );
  110.  
  111.         // here is the Mesa call 
  112.         start=system_time();
  113.         render_image();
  114.         end=system_time();        
  115.         printf("Rendering took %3.1f seconds\n",(float)((end-start)/1000000));
  116.  
  117.         // destroy the context 
  118.         OSMesaDestroyContext( ctx );
  119.     
  120.         the_window->Lock();
  121.         the_view->Draw(the_view->Bounds());
  122.         the_window->Unlock();
  123.     };
  124.  
  125.     bool QuitRequested(void)
  126.     {
  127.         // Make sure that the window closes first
  128.         if (BApplication::QuitRequested()) 
  129.         {
  130.             if (the_bitmap)    delete the_bitmap;
  131.             return TRUE;
  132.         }
  133.         return FALSE;
  134.     };
  135.             
  136.     void AboutRequested(void)
  137.     {
  138.         char str[256];
  139.         sprintf(str, "MesaDemos, ported by Tinic Urou\n<5uro@informatik.uni-hamburg.de>\nFreely distributable.");
  140.         BAlert *the_alert = new BAlert("", str, "OK");
  141.         the_alert->Go();
  142.     };
  143. };
  144.  
  145. int main( int argc, char *argv[] )
  146. {
  147.     MesaApp *the_app;
  148.  
  149.     the_app = new MesaApp();
  150.     the_app->Run();
  151.     delete the_app;
  152.  
  153.     return 0;
  154. }
  155.